﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsCorporationDAL.cs
/// 作  者：Kidy
/// 时  间：2006.11.13
/// 说  明：ClsCorporationDAL类用于企业信息相关处理
/// </summary>
public class ClsStockQueryDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有的企业信息
    /// </summary>
    /// <returns>返回企业信息数据集</returns>
    public DataSet GetCorporations(string Co_ID, string User_ID, string Ds_ID)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetDataSet", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@co_id", Co_ID);
        dat.SelectCommand.Parameters.Add("@user_id", User_ID);
        dat.SelectCommand.Parameters.Add("@ds_id", Ds_ID);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 搜索企业信息
    /// </summary>
    /// <param name="Key">搜索关键字</param>
    /// <returns>返回搜索信息数据集</returns>
    public DataSet SearchStockQuery(string Strid, string Strno,bool Check)
    {
        if (Check)
        {
            DataSet dst = new DataSet();
            SqlConnection con = new SqlConnection(strSqlConnectionString);
            SqlDataAdapter dat = new SqlDataAdapter("Pro_SearchStockQuery", con);
            dat.SelectCommand.CommandType = CommandType.StoredProcedure;
            dat.SelectCommand.Parameters.Add("@keyid", Strid);
            dat.SelectCommand.Parameters.Add("@keyno", Strno);

            try
            {
                dat.Fill(dst);
            }
            catch
            {
                dst = null;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }

            return dst;
        }
        else
        {
            DataSet dst = new DataSet();
            SqlConnection con = new SqlConnection(strSqlConnectionString);
            SqlDataAdapter dat = new SqlDataAdapter("Pro_SearchStockQueryE", con);
            dat.SelectCommand.CommandType = CommandType.StoredProcedure;
            dat.SelectCommand.Parameters.Add("@keyid", Strid);
            dat.SelectCommand.Parameters.Add("@keyno", Strno);

            try
            {
                dat.Fill(dst);
            }
            catch
            {
                dst = null;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }

            return dst;
        }
    }

    /// <summary>
    /// 检验企业编号是否存在
    /// </summary>
    /// <param name="Comp_ID">企业编号</param>
    /// <returns>返回－1检验失败，0编号不存在，大于0存在</returns>
    public int CheckUpCorporationID(string Comp_ID)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_CheckUpCorporationID", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@comp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@comp_id"].Value = Comp_ID;

        try
        {
            con.Open();

            return (int)com.ExecuteScalar();
        }
        catch
        {
            return -1;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 添加企业信息
    /// </summary>
    /// <param name="Comp_ID">企业编号</param>
    /// <param name="Comp_Oper">录入人</param>
    /// <returns>返回添加是否成功</returns>
    public Boolean AddCorporation(string Comp_ID, string Comp_name, string Comp_desc,
        string Comp_eng, string Comp_boss, string Comp_tel1, string Comp_tel2, string Comp_fax1, string Comp_fax2, string Comp_address,
        string Comp_post, string Comp_email, string Comp_website, string Comp_bank, string Comp_accno, string Comp_taxno,
        string Comp_Oper,int iComp_statu, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddCorporation", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@comp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@comp_id"].Value = Comp_ID;
        com.Parameters.Add("@comp_name", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_name"].Value = Comp_name;
        com.Parameters.Add("@comp_desc", SqlDbType.NVarChar, 255);
        com.Parameters["@comp_desc"].Value = Comp_desc;
        com.Parameters.Add("@comp_eng", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_eng"].Value = Comp_eng;
        com.Parameters.Add("@comp_boss", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_boss"].Value = Comp_boss;
        com.Parameters.Add("@comp_tel1", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_tel1"].Value = Comp_tel1;
        com.Parameters.Add("@comp_tel2", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_tel2"].Value = Comp_tel2;
        com.Parameters.Add("@comp_fax1", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_fax1"].Value = Comp_fax1;
        com.Parameters.Add("@comp_fax2", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_fax2"].Value = Comp_fax2;
        com.Parameters.Add("@comp_address", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_address"].Value = Comp_address;
        com.Parameters.Add("@comp_post", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_post"].Value = Comp_post;
        com.Parameters.Add("@comp_email", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_email"].Value = Comp_email;
        com.Parameters.Add("@comp_website", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_website"].Value = Comp_website;
        com.Parameters.Add("@comp_bank", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_bank"].Value = Comp_bank;
        com.Parameters.Add("@comp_accno", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_accno"].Value = Comp_accno;
        com.Parameters.Add("@comp_taxno", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_taxno"].Value = Comp_taxno;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 12);
        com.Parameters["@oper"].Value = Comp_Oper;
        com.Parameters.Add("@Comp_statu", SqlDbType.Int);
        com.Parameters["@Comp_statu"].Value = iComp_statu;
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = Remark;
        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改企业信息
    /// </summary>
    /// <param name="Comp_ID">企业编号</param>
    /// <param name="Comp_ID">要修改的字段</param>
    /// <param name="Comp_ID">字段值</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean UpDateCorporation(string Comp_ID, string Comp_name, string Comp_desc,
        string Comp_eng, string Comp_boss, string Comp_tel1, string Comp_tel2, string Comp_fax1, string Comp_fax2, string Comp_address,
        string Comp_post, string Comp_email, string Comp_website, string Comp_bank, string Comp_accno, string Comp_taxno,int iComp_statu,string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        //SqlCommand com = new SqlCommand("Update xtCompany set " + Field_Name + "='" + Field_Value + "' Where comp_id='" + Comp_ID + "'", con);
        SqlCommand com = new SqlCommand("Pro_UpDateCorporation", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@comp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@comp_id"].Value = Comp_ID;
        com.Parameters.Add("@comp_name", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_name"].Value = Comp_name;
        com.Parameters.Add("@comp_desc", SqlDbType.NVarChar, 255);
        com.Parameters["@comp_desc"].Value = Comp_desc;
        com.Parameters.Add("@comp_eng", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_eng"].Value = Comp_eng;
        com.Parameters.Add("@comp_boss", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_boss"].Value = Comp_boss;
        com.Parameters.Add("@comp_tel1", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_tel1"].Value = Comp_tel1;
        com.Parameters.Add("@comp_tel2", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_tel2"].Value = Comp_tel2;
        com.Parameters.Add("@comp_fax1", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_fax1"].Value = Comp_fax1;
        com.Parameters.Add("@comp_fax2", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_fax2"].Value = Comp_fax2;
        com.Parameters.Add("@comp_address", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_address"].Value = Comp_address;
        com.Parameters.Add("@comp_post", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_post"].Value = Comp_post;
        com.Parameters.Add("@comp_email", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_email"].Value = Comp_email;
        com.Parameters.Add("@comp_website", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_website"].Value = Comp_website;
        com.Parameters.Add("@comp_bank", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_bank"].Value = Comp_bank;
        com.Parameters.Add("@comp_accno", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_accno"].Value = Comp_accno;
        com.Parameters.Add("@comp_taxno", SqlDbType.NVarChar, 60);
        com.Parameters["@comp_taxno"].Value = Comp_taxno;
        //com.Parameters.Add("@oper", SqlDbType.NVarChar, 12);
        //com.Parameters["@oper"].Value = Comp_Oper;
        com.Parameters.Add("@Comp_statu", SqlDbType.Int);
        com.Parameters["@Comp_statu"].Value = iComp_statu;
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废企业信息
    /// </summary>
    /// <param name="Comp_ID">企业编号</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteCorporation(string Comp_ID, int Is_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteCorporation", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@comp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@comp_id"].Value = Comp_ID;
        com.Parameters.Add("@Is_Visible", SqlDbType.Int, 1);
        com.Parameters["@Is_Visible"].Value = Is_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 确认企业信息
    /// </summary>
    /// <param name="Comp_ID">企业编号</param>
    /// <param name="AuditStat">确认状态</param>
    /// <param name="Auditor">确认人</param>
    /// <returns>返回确认是否成功</returns>
    public Boolean AuditStatCorporation(string Comp_ID, int AuditStat, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditStatSetCorporation", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@comp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@comp_id"].Value = Comp_ID;
        com.Parameters.Add("@is_audit", SqlDbType.Int);
        com.Parameters["@is_audit"].Value = AuditStat;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}
